<?php
/**
 * Plugin Name:  EGANY Discount Offer 
 * Plugin URI: http://www.facebook.com/NguyenDuyPhong
 * Description: 'EGANY Discount Offer' help you improve discount functions, manage cart discounts easily.
 * Author: EGANY
 * Author URI: http://www.facebook.com/NguyenDuyPhong
 * Version: 1.0.0
 * Text Domain: EGANY
 * Domain Path: /languages/
 *
 * Copyright: (c) 2014, EGANY
 *
 * License: GNU General Public License v3.0
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * @package   egany_discount_offer
 * @author    PhongND
 * @category  Utility
 * @copyright Copyright (c) 2014, EGANY.
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

// Check if WooCommerce is active
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) )
	return;

// WC version check
if ( version_compare( get_option( 'woocommerce_db_version' ), '2.1', '<' ) ) {

	function egany_discount_offer_outdated_version_notice() {

		$message = sprintf(
			__( '%sEGANY Discount Offer is inactive.%s This version requires WooCommerce 2.1 or newer. Please %supdate WooCommerce to version 2.1 or newer%s', 'EGANY' ),
			'<strong>',
			'</strong>',
			'<a href="' . admin_url( 'plugins.php' ) . '">',
			'&nbsp;&raquo;</a>'
		);

		echo sprintf( '<div class="error"><p>%s</p></div>', $message );
	}

	add_action( 'admin_notices', 'egany_discount_offer_outdated_version_notice' ); 
	return;
}


/**
 * # EGANY Discount Offer Main Plugin Class
 *
 * ## Plugin Overview
 * 'EGANY Discount Offer' help you manage cart discounts easily.
 * 
 *
 * ## Admin Considerations
 *
 * 'EGANY Discount Offer' plugin include main menu 'EGANY Discount Offer' and any sub-menus like as: Periodic Discount
 *
 * ## Frontend Considerations
 *
 * 
 *
 * ## Database
 *
 * ### Global Settings
 * 
 *
 * ### Options table
 *
 *
 */
 

class egany_discount {


	/** plugin version number */
	const VERSION = '1.0.0';

	/** @var \egany_discount instance */
	public $settings;

	/** var array the active filters */
	public $filters;


	/**
	 * Initializes the plugin
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		
		
		define( 'EGANY_PLUGIN_FILE', __FILE__ );
		define( 'EGANY_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
		define( 'EGANY_VERSION', $this->VERSION );
		
		
		// load translation
		add_action( 'init', array( $this, 'load_translation' ) );
		
		
		// add_action( 'init', 'register_post_types' , 5 );
		// admin
		
		if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
		
			//enqueue scripts admin for egany_discount_offer 
			//jQuery
			wp_enqueue_script( 'jquery-ui02', plugins_url( '/assets/js/jquery-ui-1.11.2/jquery-ui.min.js', __FILE__ ) );
			wp_enqueue_style( 'jquery-ui-styles02', plugins_url('/assets/js/jquery-ui-1.11.2/jquery-ui.min.css' , __FILE__ )); // ok
			
			//WC, nono, woocommerce_admin.mon_decimal_point
			// wp_enqueue_script( 'jquery.tipTip', plugins_url( '/assets/js/jquery-tiptip/jquery.tipTip.min.js', __FILE__ ) );
			// wp_enqueue_script( 'woocommerce_admin', plugins_url( '/assets/js/admin/woocommerce_admin.min.js', __FILE__ ) );

			
			//Load scripts: js, css.
			add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) );
			
			// //admin post-type, metabox
			include_once('includes/egany-admin-post-type.php');
			include_once('includes/egany-admin-metaboxes.php');
			
			
			// load settings page
			add_filter( 'woocommerce_get_settings_pages', array( $this, 'add_settings_page' ) );

			// add a 'Configure' link to the plugin action links
			add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_plugin_action_links' ) );
			
			
			// // run every time
			// $this->install();
			
			
		}
		
		add_action( 'woocommerce_init', array( $this, 'load_customizations' ) );
	}

	/**
	 * Add settings page
	 *
	 * @since 2.0.0
	 * @param array $settings
	 * @return array
	 */
	public function add_settings_page( $settings ) {

		$settings[] = require_once( 'includes/egany_discount_offer-settings.php' );
		return $settings;
	}


	/**
	 * Load customizations after WC is loaded so the version can be checked
	 *
	 * @since 1.2.0
	 */
	public function hwif_notice($strName) {
		$message = sprintf(
			__( '%s%segany_discount_offer%s is old now. Please update WooCommerce. Description: %s', 'EGANY' ),
			'<br/>',
			'<strong>',
			'</strong>',
			 $strName. '-version: '. EGANY_VERSION . '-' .EGANY_PLUGIN_BASENAME
		);
		// $strName
		echo $message;
	}
	public function load_customizations() {

		// nono, load filter names and values
		$chk01 = get_option('hwif_chkOrder_DoHooks');
		$desc = get_option('hwif_txtMessage');
		$desc = !empty($desc) ? $desc : '_ empty _' ;
		if ($chk01 == 'yes')
		{
			//admin.less
			// for front-end.
			// add_action( 'admin_notices', call_user_func_array(array( $this, 'hwif_notice'), array($desc))); // PhongND 2014/11/23 
			
		}
		else
		{
			// add_action( 'admin_notices', call_user_func_array(array( $this, 'hwif_notice'), array("Nothing..."))); // PhongND 2014/11/23 
		}
	}
	
	
	
			
	/**
	 * Handle localization, WPML compatible
	 *
	 * @since 1.1.0
	 */
	public function load_translation() {

		// localization in the init action for WPML support
		load_plugin_textdomain( 'EGANY', false, dirname( plugin_basename( __FILE__ ) ) . '/i18n/languages' );
	}


	/** Frontend methods ******************************************************/
	



	/** Admin methods ******************************************************/

	/**
	 * Return the plugin action links.  This will only be called if the plugin
	 * is active.
	 *
	 * @since 1.0.0
	 * @param array $actions associative array of action names to anchor tags
	 * @return array associative array of plugin action links
	 */
	public function add_plugin_action_links( $actions ) {

		$custom_actions = array(
			'configure' => sprintf( '<a href="%s">%s</a>', admin_url( 'edit.php?post_type=periodic_discount' ), __( 'Configure', 'EGANY' ) ),
			'faq'       => sprintf( '<a href="%s">%s</a>', 'http://facebook.com/NguyenDuyPhong/', __( 'FAQ', 'EGANY' ) ),
			'support'   => sprintf( '<a href="%s">%s</a>', 'http://facebook.com/NguyenDuyPhong/', __( 'Support', 'EGANY' ) ),
		);

		// add the links to the front of the actions list
		return array_merge( $custom_actions, $actions );
	}


	/** Lifecycle methods ******************************************************/

	/**
	 * Add scripts
	 */
	public function scripts() {
		global $woocommerce; 
		
		
		wp_enqueue_style( 'egany_discount-style', plugins_url( 'assets/css/style.css', __FILE__ ) );
		wp_enqueue_script( 'egany_discount-script', plugins_url( '/assets/js/egany_discountasd.js', __FILE__ ) ); // ok
		// wp_register_script( 'egany_discount-script', plugins_url( '/assets/js/egany_discount.js', __FILE__ ) ); // ok
		
		
		// wp_register_script( 'wc-admin-meta-boxes', plugins_url . '/assets/js/admin/meta-boxes' .'.min'. '.js', __FILE__ ) array( 'jquery', 'jquery-ui-datepicker', ...??? ), WC_VERSION ); // nono 
	}

}


/**
 * The egany_discount global object
 * @name $egany_discount
 * @global egany_discount $GLOBALS['egany_discount']
 */
$GLOBALS['egany_discount'] = new egany_discount();
